import { defineStore } from 'pinia'
import {reactive,toRefs } from 'vue'; 
import arrays from '../utils/arrays.js'
import helper from '../utils/helper.js'
import REGISTER from '../register/index.js'

const coreStore = defineStore('core', () => {
	const state = reactive({
		name:"DogStar Windows 桌面 UI",
		dateFormat: 'YYYY-MM-DD',
		startMenu: false,
		sidebar:false,
		wallpaper: './wall/wall-1.jpg',
		themeColor: 'linear-gradient(0deg, rgba(100, 181, 246, 1) 0%, rgba(0, 0, 0, 1) 100%)',
		contextMenu: {
			show: false,
			type: 'wall',
			x: 0,
			y: 0,
			data: {}
		},
		applications: [],
		tasks:[],
	});
		
	const init = function() {
		this.getThemeColor();
		let wallpaper = helper.getLocalstorage('wallpaper', "");
		state.wallpaper = wallpaper;
		state.applications = []
		REGISTER.application.forEach(item => {
			let app = {}
			if (item.sublist || item.type === 'folder') {
				item.sublist.forEach(subitem => {
					app = {}
					app.id = subitem.id
					app.name = subitem.name
					app.icon = subitem.icon
					app.width = subitem.width > 0 ? subitem.width : 0
					app.height = subitem.height > 0 ? subitem.height : 0
					app.page = subitem.page
					app.resizable = helper.ObjectNotNull(subitem.resizable) ? subitem.resizable : true
					app.link = helper.ObjectNotNull(subitem.link) ? subitem.link :false 
					app.selected = false
					state.applications.push(app)
				})
			} else {
				app.id = item.id
				app.name = item.name
				app.icon = item.icon
				app.width = item.width > 0 ? item.width : 0
				app.height = item.height > 0 ? item.height : 0
				app.page = item.page
				app.link = helper.ObjectNotNull(item.link) ?  item.link :false 
				app.resizable = helper.ObjectNotNull(item.resizable) ? item.resizable : true
				app.selected = false
				state.applications.push(app)
			}
		})
	}
	
	const openApplication = function(id){
		let i = arrays.findIndexById(state.tasks,id);
		if(i < 0){
			let object = state.applications.filter(t => t.id == id)[0]
			let task = reactive({})
			task.title = object.name
			task.id = object.id
			task.icon = object.icon
			task.width = object.width
			task.height = object.height
			task.page = helper.ObjectNotNull(object.page) ? object.page : ""
			task.resizable = helper.ObjectNotNull(object.resizable) ? object.resizable : true
			task.min = false
			task.focus = true
			task.date = new Date()
			task.lastDate = new Date()
			state.tasks.push(task);
			applicationSort();
		} else {
			applicationFocus(id);
		}
	}
	
	const closeApplication = function(id) {
		let i = arrays.findIndexById(state.tasks,id)
		if(i < state.tasks.length){
			arrays.deleteByIndex(state.tasks,i)
		}
	}
	
	const applicationFocus = function(id){
		let i = arrays.findIndexById(state.tasks,id)
		if(i < state.tasks.length) {
			let app = state.tasks[i]
			app.lastDate = new Date()
		}
		applicationSort();
	}
	
	const applicationSort = function() {
		state.tasks.sort((a, b) => {
			return b.lastDate - a.lastDate
		})
	}
	
	const applicationMinSwitch = function(id) {
		let i = arrays.findIndexById(state.tasks,id)
		if(i < state.tasks.length){
			let app = state.tasks[i]
			if(app.focus == true){
				app.min = true
				app.lastDate = state.tasks[state.tasks.length - 1].lastDate - 1
			} else if(app.min == true){
				app.min = false
				app.lastDate = new Date()
			} else {
				app.lastDate = new Date()
			}
		}
		applicationSort();
	}
	
	const selectIcon = function(id) {
		state.applications.forEach(item => {
			if (id == item.id) {
				item.selected = true
			} else {
				item.selected = false
			}
		})
		if (state.startMenu) {
			state.startMenu = false
		}
	}
	
	const openMenu = function() {
		state.startMenu = !state.startMenu
	}
	
	
	const setWallpaper = function(url){
		state.wallpaper = url;
		helper.setLocalstorage('wallpaper', url)
	}
	
	const showContextMenu = function(x,y,type,data) {
		state.contextMenu.show = true;
		state.contextMenu.x = x;
		state.contextMenu.y = y;
		state.contextMenu.type = type;
		state.contextMenu.data = data;
	}
	
	const closeContextMenu = function() {
		if(state.contextMenu.show){
			state.contextMenu.show = false;
			state.contextMenu.x = 0;
			state.contextMenu.y = 0;
			state.contextMenu.type = '';
			state.contextMenu.data = {};
		}
	}
	
	const switchSidebar = function(flag) {
		if(helper.ObjectNotNull(flag)){
			state.sidebar = flag
		} else {
			state.sidebar = !state.sidebar
		}
	}
	
	const setThemeColor = function(color) {
		state.themeColor = color;
		helper.setLocalstorage('m_themeColor',color);
		document.documentElement.setAttribute('style', '--theme-color: ' + color);
	}
	
	const getThemeColor = function() {
		let themeColor = helper.getLocalstorage('m_themeColor',"");
		if(helper.StringIsNull(themeColor)) {
			document.documentElement.setAttribute('style', '--theme-color: ' + state.themeColor);
			return state.themeColor;
		} else {
			document.documentElement.setAttribute('style', '--theme-color: ' + themeColor);
			return themeColor;
		}
	}
	
	return {
		init,
		openApplication,closeApplication,applicationMinSwitch,applicationFocus,
		selectIcon,showContextMenu,closeContextMenu,
		setWallpaper,switchSidebar,openMenu,setThemeColor,getThemeColor,
		...toRefs(state)
	}
});
export default coreStore